﻿using Dapper;
using DapperExtensions;
using DapperExtensions.Mapper;
using DapperExtensions.Sql;
using Logistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Logistics_Dao
{
    public class WuliuCompanyDao
    {
        private SqlGeneratorImpl sqlGenerator;
        public WuliuCompanyDao()
        {
            var config = new DapperExtensionsConfiguration(typeof(AutoClassMapper<>), new List<Assembly>(), new MySqlDialect());
            sqlGenerator = new SqlGeneratorImpl(config);
            DapperExtensions.DapperExtensions.Configure(config);
        }

        public PageData<wuliu_wlcompany_base> GetList(IPredicate predicate, int page, int pageSize)
        {
            var sql = "select * from wuliu_wlcompany_base where 1=1 ";

            using (var conn = ConnectionHelper.OpenConnection())
            {
                var pageDate = new PageData<wuliu_wlcompany_base>();
                DynamicParameters dynamicParameters = new DynamicParameters();
                var whereSql = GetWhereSql(predicate, dynamicParameters);
                
                if (string.IsNullOrEmpty(whereSql))
                    sql += " and " + whereSql;
                sql += "order by wuliu_id asc limit @pageIndex,@pageSize";
                dynamicParameters.Add("pageIndex", page*pageSize);
                dynamicParameters.Add("pageSize", pageSize);
                var count = conn.Query<wuliu_wlcompany_base>(sql, dynamicParameters).Count();
                pageDate.Items = conn.Query<wuliu_wlcompany_base>(sql, dynamicParameters).ToList();
                pageDate.TotalNum = count;
                pageDate.CurrentPage = page;
                pageDate.TotalPageCount = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
                return pageDate;
            }
        }

        private string GetWhereSql(IPredicate predicate, DynamicParameters dynamicParameters)
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            var wherSql = predicate.GetSql(sqlGenerator, parameters);


            foreach (var parameter in parameters)
            {
                dynamicParameters.Add(parameter.Key, parameter.Value);
            }
            return wherSql;
        }
        /// <summary>
        /// 通过主键获取物流公司信息
        /// </summary>
        /// <param name="wuliu_id"></param>
        /// <returns></returns>
        public wuliu_wlcompany_base Get(int wuliu_id)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                return conn.Get<wuliu_wlcompany_base>(wuliu_id);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(wuliu_wlcompany_base model)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                StringBuilder str = new StringBuilder();
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update wuliu_wlcompany_base set ");
                if (model.wuliu_name.IfNotNull())
                    strSql.Append("wuliu_name=@wuliu_name,");
                if (model.city_id.IfNotNull())
                    strSql.Append("city_id=@city_id,");
                if (model.address.IfNotNull())
                    strSql.Append("address=@address,");
                if (model.phone.IfNotNull())
                    strSql.Append("phone=@phone,");
                if (model.name.IfNotNull())
                    strSql.Append("name=@name,");
                if (model.state.IfNotNull())
                    strSql.Append("state=@state,");
                if (model.create_time.IfNotNull())
                    strSql.Append("create_time=@create_time,");
                if (model.tax.IfNotNull())
                    strSql.Append("tax=@tax,");
                if (model.wuliu_no.IfNotNull())
                    strSql.Append("wuliu_no=@wuliu_no,");
                if (model.tel.IfNotNull())
                    strSql.Append("tel=@tel,");
                if (model.bond.IfNotNull())
                    strSql.Append("bond=@bond,");
                if (model.logo.IfNotNull())
                    strSql.Append("logo=@logo,");
                if (model.certificate3.IfNotNull())
                    strSql.Append("certificate3=@certificate3,");
                if (model.certificate2.IfNotNull())
                    strSql.Append("certificate2=@certificate2,");
                if (model.certificate1.IfNotNull())
                    strSql.Append("certificate1=@certificate1,");
                if (model.information.IfNotNull())
                    strSql.Append("information=@information,");
                if (model.signed_status.IfNotNull())
                    strSql.Append("signed_status=@signed_status,");
                if (model.fee_status.IfNotNull())
                    strSql.Append("fee_status=@fee_status,");
                if (model.fee_num.IfNotNull())
                    strSql.Append("fee_num=@fee_num,");
                if (model.bond_status.IfNotNull())
                    strSql.Append("bond_status=@bond_status,");
                if (model.orders_time.IfNotNull())
                    strSql.Append("orders_time=@orders_time,");
                if (model.type.IfNotNull())
                    strSql.Append("type=@type,");
                if (model.flag.IfNotNull())
                    strSql.Append("flag=@flag,");
                if (model.approve_flag.IfNotNull())
                    strSql.Append("approve_flag=@approve_flag,");
                if (model.apply_num1.IfNotNull())
                    strSql.Append("apply_num1=@apply_num1,");
                if (model.apply_num2.IfNotNull())
                    strSql.Append("apply_num2=@apply_num2,");
                if (model.city_name.IfNotNull())
                    strSql.Append("city_name=@city_name,");
                if (model.apply_mon_num1.IfNotNull())
                    strSql.Append("apply_mon_num1=@apply_mon_num1,");
                if (model.apply_mon_num2.IfNotNull())
                    strSql.Append("apply_mon_num2=@apply_mon_num2,");
                if (model.city_id_x.IfNotNull())
                    strSql.Append("city_id_x=@city_id_x,");
                if (model.business_scope.IfNotNull())
                    strSql.Append("business_scope=@business_scope,");
                if (model.business_scope_id1.IfNotNull())
                    strSql.Append("business_scope_id1=@business_scope_id1,");
                if (model.business_scope_id2.IfNotNull())
                    strSql.Append("business_scope_id2=@business_scope_id2,");
                if (model.business_scope_id3.IfNotNull())
                    strSql.Append("business_scope_id3=@business_scope_id3,");
                if (model.business_scope_id4.IfNotNull())
                    strSql.Append("business_scope_id4=@business_scope_id4,");
                if (model.business_scope_name1.IfNotNull())
                    strSql.Append("business_scope_name1=@business_scope_name1,");
                if (model.business_scope_name2.IfNotNull())
                    strSql.Append("business_scope_name2=@business_scope_name2,");
                if (model.business_scope_name3.IfNotNull())
                    strSql.Append("business_scope_name3=@business_scope_name3,");
                if (model.business_scope_name4.IfNotNull())
                    strSql.Append("business_scope_name4=@business_scope_name4");
                str.Append(strSql.ToString().TrimEnd(','));
                str.Append(" where wuliu_id=@wuliu_id ");
                return conn.Execute(str.ToString(), model);
            }
        }

        /// <summary>
        /// 通过物流编号获取物流公司信息
        /// </summary>
        /// <param name="wuliu_no"></param>
        /// <returns></returns>
        public wuliu_wlcompany_base GetWuliu(string wuliu_no)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {

                var sql = "select * from wuliu_wlcompany_base where wuliu_no=@wuliu_no and state=1";
                return conn.Query<wuliu_wlcompany_base>(sql, new { wuliu_no }).FirstOrDefault();
            }
        }
        /// <summary>
        /// 通过物流编号获取物流公司信息
        /// </summary>
        /// <param name="wuliu_no"></param>
        /// <returns></returns>
        public List<wuliu_wlcompany_base> GetWuliuList()
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                var sql = "select * from wuliu_wlcompany_base ";
                return conn.Query<wuliu_wlcompany_base>(sql).ToList();
            }
        }
    }
}
